package com.abc.robot01.vivemodel

import android.util.Log
import androidx.compose.runtime.mutableStateOf
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.abc.robot01.retrofit.RetrofitService
import com.abc.robot01.retrofit.bean.Condition
import com.abc.robot01.retrofit.bean.FetchDataRequest9
import com.abc.robot01.retrofit.bean.HouseType
import com.abc.robot01.retrofit.bean.Item7
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.launch
import org.json.JSONObject


class FangAnKuViewModel : ViewModel() {

    val TAG = "FangAnKuViewModel"

    // MutableStateFlow 用来保存当前的列表状态
    private val _questionList = MutableStateFlow<List<Item7>>(emptyList())
    val questionList: StateFlow<List<Item7>> = _questionList

    //喜好
    private val _question2List = MutableStateFlow<List<HouseType>>(mutableListOf(HouseType()))

    val question2List: StateFlow<List<HouseType>> = _question2List

    //选择户型
    private val _question3List = MutableStateFlow<List<HouseType>>(emptyList())
    val question3List: StateFlow<List<HouseType>> = _question3List

    //风格
    private val _question4List = MutableStateFlow<List<HouseType>>(mutableListOf(HouseType()))
    val question4List: StateFlow<List<HouseType>> = _question4List

    /**
     * 当前是否是公库.
     * */
    val isPublic = mutableStateOf(false)

    val name = mutableStateOf("")

    val interestsCode = mutableStateOf("")
    val interests = mutableStateOf("不限")

    //风格
    val styleCode = mutableStateOf("")
    val style = mutableStateOf("不限")

    //选择户型
    val styleCode2 = mutableStateOf("")
    val style2 = mutableStateOf("不限")

    fun reset() {
        interests.value = "不限"
        style.value = "不限"
        style2.value = "不限"
        interestsCode.value = ""
        styleCode.value = ""
        styleCode2.value = ""
    }

    // 模拟网络分批返回数据的函数
    fun loadData(
        isPublic2: Boolean = isPublic.value,
        name2: String = name.value,
        interests2: String = interestsCode.value,
        style: String = styleCode.value,
        useTypeCodes: String = styleCode2.value
    ) {
        Log.e(TAG, "是否公库:$isPublic2")
        Log.e(TAG, "名称:$name2")
        Log.e(TAG, "喜好:$interests2")
        Log.e(TAG, "选择户型:$useTypeCodes")
        Log.e(TAG, "风格:$style")

        viewModelScope.launch {
            _questionList.value = emptyList()
//            getToken()
            val result = RetrofitService.instance.getDesignPackages(
                userId = Robot.userId,
                companyId = Robot.companyId,
                accessToken = Robot.accessToken, request = FetchDataRequest9(
                    condition = Condition(
                        isPublic = isPublic2,
                        name = name2,
                        interests = interests2,
                        styles = style, useTypeCodes = useTypeCodes
                    )
                )
            ).string()

            Log.e(TAG, "getDesignPackages:  result:$result")
            val jsonObject2 = JSONObject(result)
            if (jsonObject2.getInt("code") == 200) {
                val data = jsonObject2.getJSONObject("data");
                val array = data.getJSONArray("list")
                repeat(array.length()) {
                    val item = array.getJSONObject(it);
                    val name = item.getString("name")
                    val id = item.getString("id")
                    val price = item.getString("price")
                    val thumbnail = item.getString("thumbnail")
                    val renderUrl = item.getString("renderUrl")
                    Log.e(TAG, "name:$name")
                    _questionList.value += Item7(
                        id = id,
                        name = name,
                        price = price + "元",
                        thumbnail = thumbnail,
                        renderUrl = renderUrl
                    )
                }
            }
        }
    }

    fun loadData2() {
        //喜好
        viewModelScope.launch {
            Log.e(TAG, "喜好")
            val s = RetrofitService.instance.getSysDicByType(
                accessToken = Robot.accessToken,
                type = "INTEREST"
            )
            _question2List.value = s.data
            Log.e(TAG, "喜好:$s")


            Log.e(TAG, "选择户型")
            val s2 = RetrofitService.instance.getSysDicByType(
                accessToken = Robot.accessToken,
                type = "JPFA_SYLX"
            ).also {
                Log.e(TAG, "选择户型 size:" + it.data.size)
                Log.e(TAG, "选择户型:${it.data}")
            }
            _question3List.value = s2.data

            Log.e(TAG, "风格")
            val s1 = RetrofitService.instance.getSysDicByType(
                accessToken = Robot.accessToken,
                type = "STYLE"
            )
            _question4List.value = s1.data
            Log.e(TAG, "风格 size:" + s1.data.size)
            Log.e(TAG, "风格:$s")
        }
    }
}

